﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace Basefr.Runtime.Helpers
{
    public static class PermissionHelper
    {
        public static List<KeyValuePair<string, string>> GetList<TPermissionDefinition>()
        {
            return typeof(TPermissionDefinition).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
                .Select(p => new KeyValuePair<string, string>(p.GetRawConstantValue().ToString(), p.GetCustomAttribute<DescriptionAttribute>().Description)).ToList();
        }

        public static string GetDescriptionByValue<TPermissionDefinition>(string value)
        {
            return GetList<TPermissionDefinition>().FirstOrDefault(p => p.Key == value).Value;
        }
    }
}
